package interchange.data;

import interchange.domain.Servicio;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import interchange.data.ConnectionManager;
import interchange.utiles.UIDGenerator;


public class JDBCServicioDAO implements IServicioDAO {

	public Servicio select(Connection conn, String OIDservicio) {
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        Servicio s = null;
	        String sql = "SELECT * FROM Servicio WHERE (OID = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, OIDservicio);
	            result = stmt.executeQuery();

	            result.next();
	            s = new Servicio();
	            s.setDescripcion(result.getString("descripcion"));
	            s.setCategoria(result.getString("categoria"));
	            s.setServicioID(result.getString("servicioID"));
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return s;
	}

	public List<Servicio> selectAllServicios() {
		 Connection conn = ConnectionManager.getInstance().checkOut();

	      PreparedStatement stmt = null;
	        List<Servicio> searchResults = new LinkedList<Servicio>();
	        ResultSet result = null;

	        try {

	            String sql = "SELECT * FROM Servicio";
	            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
	            stmt.executeQuery();
	            result = stmt.executeQuery();

	            while (result.next()) {
	                Servicio temp = new Servicio();
	                temp.setServicioID(result.getString("IDServicio"));
	                temp.setDescripcion(result.getString("descripcion"));
	                temp.setCategoria(result.getString("categoria"));
	                searchResults.add(temp);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (result != null)
	                    result.close();
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }

	        }
	        return searchResults;
		
	}

	public String selectOIDServicio(Connection conn, String IDservicio) {
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        String oids = null;
	        String sql = "SELECT * FROM Servicio WHERE (IDServicio = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, IDservicio);
	            result = stmt.executeQuery();

	            result.next();
	            oids = result.getString("OID");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oids;
	    }

	public void delete(Connection conn, String IDServicio){
		
		 String sql = "DELETE FROM Servicio WHERE (OIDServicio = ?) ";
	        PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, IDServicio);
	            stmt.executeUpdate();
	        } catch (SQLException e) {
	            System.err.println("Message: " + e.getMessage());
	            System.err.println("SQLState: " + e.getSQLState());
	            System.err.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
		
	}

	public void insert(Connection conn, String OIDServicio, Servicio s) {
		String oids = UIDGenerator.getInstance().getKey();

		String sql = "INSERT INTO Servicio ( OIDServicio, idServicio, descripcion, categoria) VALUES (?, ?, ?, ?) ";
	
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, oids);
            stmt.setString(2, s.getServicioID());
            stmt.setString(3, s.getDescripcion());
            stmt.setString(4, s.getCategoria());
            
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	

	
}
	
	


